﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using QuanLyThuVien.DataAccess;
using QuanLyThuVien.Model;

namespace QuanLyThuVien.View
{
    public partial class FormDocGia : Form
    {
        DocGiaAccess docgiaAccess = new DocGiaAccess();
        DataTable dsDocgia = new DataTable();
        public FormDocGia()
        {
            InitializeComponent();
        }
        private void FormDocGia_Load(object sender, EventArgs e)
        {
            dsDocgia = docgiaAccess.layToanboTTDocgia();
            dataGridView1.DataSource = dsDocgia;
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            DocGia dg = new DocGia(txtMaThe.Text, txtHovaten.Text, DateTime.Parse(dtNgaysinh.Text), txtGioitinh.Text, txtDiaChi.Text, txtSDT.Text);
            if (docgiaAccess.suaDocGia(dg)==0  ) { MessageBox.Show("Mã thẻ không tồn tại !"); }
            else
            {
                MessageBox.Show("Sửa thành công !");
            }
            FormDocGia_Load(sender, e);
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            txtMaThe.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            txtHovaten.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            dtNgaysinh.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
            txtGioitinh.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
            txtDiaChi.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
            txtSDT.Text = dataGridView1.CurrentRow.Cells[5].Value.ToString();
         


        }

        private void txt_TimSach_TextChanged(object sender, EventArgs e)
        {
              DataView dataView = new DataView();
            if (cbb_TimDG.SelectedItem==cbb_TimDG.Items[0])
            {
                dataView =docgiaAccess.timkiemMadg(txt_TimDG.Text);
                dataGridView1.DataSource = dataView;
            }
            else
            {
                dataView = docgiaAccess.timkiemTendg(txt_TimDG.Text);
                dataGridView1.DataSource = dataView;
            }
        }

        private void cbb_TimDG_SelectedIndexChanged(object sender, EventArgs e)
        {
        
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
             DocGia dg=new DocGia(txtMaThe.Text, txtHovaten.Text, DateTime.Parse(dtNgaysinh.Text), txtGioitinh.Text, txtDiaChi.Text, txtSDT.Text);
            if(docgiaAccess.xoadg(dg)==0)
            {
                MessageBox.Show("Mã thẻ không tồn tại ");
            }
            else
            {
                MessageBox.Show("Xóa thành công");
            }
              FormDocGia_Load(sender, e);
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            DocGia dg = new DocGia(txtMaThe.Text, txtHovaten.Text, DateTime.Parse(dtNgaysinh.Text), txtGioitinh.Text, txtDiaChi.Text, txtSDT.Text);
            if (docgiaAccess.themDocGia(dg) == 0) { MessageBox.Show("Mã thẻ đã tồn tại !"); }
            else
            {
                MessageBox.Show("Thêm thành công !");
            }
            FormDocGia_Load(sender, e);
        }
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

            if (checkBox1.Checked == true)
            {
                txtMaThe.ReadOnly = false;
                //cbbLopHoc.Enabled = true;
                btnThem.Enabled = true;
            }
            else
            {
                txtMaThe.ReadOnly = true;
                //cbbLopHoc.Enabled = false;
                btnThem.Enabled = false;
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn thoát không ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1) == DialogResult.OK)
            {
                this.Close();
            }
        }
    }
}

       

